package com.fastwire.carriere.inventory.entity;

import java.util.List;
import java.util.Set;

import org.tmforum.mtop.fmw.xsd.gen.v1.AliasNameListType.Alias;
import org.tmforum.mtop.nrb.xsd.lp.v1.LayeredParametersType;
import org.tmforum.mtop.nrf.xsd.cc.v1.CrossConnectType.AEndRefList;
import org.tmforum.mtop.nrf.xsd.cc.v1.CrossConnectType.ZEndRefList;

import com.fastwire.carriere.common.constant.HuaweiInventoryConstants;
import com.google.common.collect.Sets;

// TODO: Auto-generated Javadoc
/**
 * The Class CrossConnect.
 */
public class CrossConnect extends NetworkEntity {

	/** The description. */
	protected String description;

	/** The is active. */
	protected String isActive;

	/** The administrative state. */
	protected String administrativeState;

	/** The direction. */
	protected String direction;

	/** The is fixed. */
	protected String isFixed;

	/** The aend tp. */
	protected SubTerminationPoint aendTp;

	/** The zend tp. */
	protected SubTerminationPoint zendTp;

	/** The trans parameters. */
	private Set<TransmissionParameter> transParameters;

	/**
	 * Gets the trans parameters.
	 * 
	 * @return the trans parameters
	 */
	public Set<TransmissionParameter> getTransParameters() {
		return transParameters;
	}

	/**
	 * Sets the trans parameters.
	 * 
	 * @param transParameters
	 *            the new trans parameters
	 */
	public void setTransParameters(Set<TransmissionParameter> transParameters) {
		this.transParameters = transParameters;
	}

	/**
	 * Sets the aend ctp.
	 * 
	 * @param aendRefList
	 *            the new aend ctp
	 */
	public void setAendCTP(AEndRefList aendRefList) {
		this.aendTp = new SubTerminationPoint(aendRefList);
	}

	/**
	 * Sets the zend ctp.
	 * 
	 * @param zEndRefList
	 *            the new zend ctp
	 */
	public void setZendCTP(ZEndRefList zEndRefList) {
		this.zendTp = new SubTerminationPoint(zEndRefList);
	}

	/**
	 * Gets the description.
	 * 
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Sets the description.
	 * 
	 * @param description
	 *            the new description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Gets the direction.
	 * 
	 * @return the direction
	 */
	public String getDirection() {
		return direction;
	}

	/**
	 * Sets the direction.
	 * 
	 * @param direction
	 *            the new direction
	 */
	public void setDirection(String direction) {
		this.direction = direction;
	}

	/**
	 * Gets the checks if is fixed.
	 * 
	 * @return the checks if is fixed
	 */
	public String getIsFixed() {
		return isFixed;
	}

	/**
	 * Sets the checks if is fixed.
	 * 
	 * @param isFixed
	 *            the new checks if is fixed
	 */
	public void setIsFixed(String isFixed) {
		this.isFixed = isFixed;
	}

	/**
	 * Gets the checks if is active.
	 * 
	 * @return the checks if is active
	 */
	public String getIsActive() {
		return isActive;
	}

	/**
	 * Sets the checks if is active.
	 * 
	 * @param isActive
	 *            the new checks if is active
	 */
	public void setIsActive(String isActive) {
		this.isActive = isActive;
	}

	/**
	 * Gets the administrative state.
	 * 
	 * @return the administrative state
	 */
	public String getAdministrativeState() {
		return administrativeState;
	}

	/**
	 * Sets the administrative state.
	 * 
	 * @param administrativeState
	 *            the new administrative state
	 */
	public void setAdministrativeState(String administrativeState) {
		this.administrativeState = administrativeState;
	}

	/**
	 * Sets the ems native name.
	 * 
	 * @param aliseRefList
	 *            the alise ref list
	 * @param isAlias
	 *            the is alias
	 */
	public void setEmsNativeName(String aliseRefList, boolean isAlias) {
		String[] emsNameListStrings = aliseRefList.split("::");

		String[] emsNames = emsNameListStrings[0].split("=");
		this.emsNativeName = emsNames[1];

		String[] descs = emsNameListStrings[1].split("=");
		this.description = descs[1];

	}

	/**
	 * Sets the alias.
	 * 
	 * @param aliasList
	 *            the new alias
	 */
	public void setAlias(List<Alias> aliasList) {
		if (aliasList != null) {

			for (Alias alias : aliasList) {
				String name = alias.getAliasName();
				String value = alias.getAliasValue();

				if ("NativeEMSName".equals(name)) {
					setEmsNativeName(value);
				} else if ("Description".equals(name)) {
					setDescription(value);

				}

			}

		}

	}

	/**
	 * Sets the trans parameters.
	 * 
	 * @param LayeredtransParameters
	 *            the layeredtrans parameters
	 * @param isEnabled
	 *            the is enabled
	 */
	public void setTransParameters(
			List<LayeredParametersType> LayeredtransParameters,
			boolean isEnabled) {
		if (LayeredtransParameters == null) {
			return;
		}

		Set<TransmissionParameter> transmissionParameters = Sets.newHashSet();
		for (LayeredParametersType layeredParametersType : LayeredtransParameters) {
			TransmissionParameter transmissionParameter = new TransmissionParameter(
					layeredParametersType);
			transmissionParameters.add(transmissionParameter);
		}
		setTransParameters(transmissionParameters);

	}

	/**
	 * Gets the aend tp.
	 * 
	 * @return the aend tp
	 */
	public SubTerminationPoint getAendTp() {
		return aendTp;
	}

	/**
	 * Sets the aend tp.
	 * 
	 * @param aendTp
	 *            the new aend tp
	 */
	public void setAendTp(SubTerminationPoint aendTp) {
		this.aendTp = aendTp;
	}

	/**
	 * Gets the next hop.
	 * 
	 * @return the next hop
	 */
	public String getNextHop() {

		String nextHop = null;
		TransmissionParameter transmissionParameter = getTransmissionParameterbyLR(HuaweiInventoryConstants.LayerRate_MPLS_Path);
		if (transmissionParameter != null) {
			nextHop = transmissionParameter
					.getParameter(HuaweiInventoryConstants.CC_NextHop);
		}

		return nextHop;
	}

	/**
	 * Gets the zend tp.
	 * 
	 * @return the zend tp
	 */
	public SubTerminationPoint getZendTp() {
		return zendTp;
	}

	/**
	 * Sets the zend tp.
	 * 
	 * @param zendTp
	 *            the new zend tp
	 */
	public void setZendTp(SubTerminationPoint zendTp) {
		this.zendTp = zendTp;
	}

	/**
	 * Gets the transmission parameterby lr.
	 * 
	 * @param layerRate
	 *            the layer rate
	 * @return the transmission parameterby lr
	 */
	public TransmissionParameter getTransmissionParameterbyLR(String layerRate) {
		for (TransmissionParameter tParameter : transParameters) {
			if (layerRate.equals(tParameter.getLayerRate())) {
				return tParameter;
			}
		}

		return null;
	}

}
